package com.app.couponsharelife.ui.listener
import android.graphics.Canvas
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.RecyclerView
import com.app.couponsharelife.utils.CardConfig.MAX_SHOW_COUNT
import com.app.couponsharelife.utils.CardConfig.SCALE_GAP
import com.app.couponsharelife.utils.CardConfig.TRANS_Y_GAP

/**
 *Created by xct on 2019/1/9
 *Description:
 */
open class RenRenCallback(private var mRv: RecyclerView, private var mAdapter: RecyclerView.Adapter<RecyclerView.ViewHolder>,
                     private var mDatas:ArrayList<Any>): ItemTouchHelper.SimpleCallback(0,
        ItemTouchHelper.DOWN or ItemTouchHelper.UP or ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT) {
    //水平方向是否可以被回收掉的阈值
    fun getThreshold(viewHolder: RecyclerView.ViewHolder): Float {
        //2016 12 26 考虑 探探垂直上下方向滑动，不删除卡片，这里参照源码写死0.5f
        return mRv.width * /*getSwipeThreshold(viewHolder)*/ 0.5f
    }

    override fun onMove(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, target: RecyclerView.ViewHolder): Boolean {
        return false
    }

    override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
        //Log.e("swipecard", "onSwiped() called with: viewHolder = [" + viewHolder + "], direction = [" + direction + "]");
        //rollBack(viewHolder);
        //★实现循环的要点
        val remove = mDatas.removeAt(viewHolder.layoutPosition)
        mDatas.add(0, remove)
        mAdapter.notifyDataSetChanged()
    }

    override fun onChildDraw(c: Canvas, recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, dX: Float, dY: Float, actionState: Int, isCurrentlyActive: Boolean) {
        super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive)
        //Log.e("swipecard", "onChildDraw()  viewHolder = [" + viewHolder + "], dX = [" + dX + "], dY = [" + dY + "], actionState = [" + actionState + "], isCurrentlyActive = [" + isCurrentlyActive + "]");
        //人人影视的效果
        //if (isCurrentlyActive) {
        //先根据滑动的dxdy 算出现在动画的比例系数fraction
        val swipValue = Math.sqrt((dX * dX + dY * dY).toDouble())
        var fraction = swipValue / getThreshold(viewHolder)
        //边界修正 最大为1
        if (fraction > 1) {
            fraction = 1.0
        }
        //对每个ChildView进行缩放 位移
        val childCount = recyclerView.childCount
        for (i in 0 until childCount) {
            val child = recyclerView.getChildAt(i)
            //第几层,举例子，count =7， 最后一个TopView（6）是第0层，
            val level = childCount - i - 1
            if (level > 0) {
                child.scaleX = (1 - SCALE_GAP * level + fraction * SCALE_GAP).toFloat()

                if (level < MAX_SHOW_COUNT - 1) {
                    child.scaleY = (1 - SCALE_GAP * level + fraction * SCALE_GAP).toFloat()
                    child.translationY = (TRANS_Y_GAP * level - fraction * TRANS_Y_GAP).toFloat()
                } else {
                    //child.setTranslationY((float) (mTranslationYGap * (level - 1) - fraction * mTranslationYGap));
                }
            }
        }
    }
}